﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Detai.Web.ViewModels;
using System.Configuration;
using System.IO;
using Detai.Admin.Models;
using Detai.Lib.Common;
using Detai.Lib.Helpers;

namespace Detai.Web.Controllers
{
    [RoleHelper(Roles =RoleSystem.Administrator)]
    public class SystemController : Controller
    {
        //
        // GET: /System/

        public ActionResult Backup()
        {
            SystemViewModels s = new SystemViewModels();
            s.Server = ConfigurationManager.AppSettings["BkServer"];
            s.Database = ConfigurationManager.AppSettings["BkDatabase"];
            s.UserId = ConfigurationManager.AppSettings["BkUserId"];
            s.Password = ConfigurationManager.AppSettings["BkPassword"];
            return View(s);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Backup(SystemViewModels s) {
            try
            {
                UpdateModel(s);
                string server = s.Server;
                string db = s.Database;
                string uid = s.UserId;
                string pass = s.Password;
                string path = ConfigurationManager.AppSettings["PathBackup"];
                if (!Directory.Exists(path))
                {
                    // If it doesn't then we just create it before going any further
                    Directory.CreateDirectory(path);
                }
                string rootFolder = path + db + DateTime.Now.ToString("yyyyMMdd-HH") + ".bak";
                DatabaseManager.BackupDatabase(db, uid, pass, server, rootFolder);
                ViewData["MessageInfo"] = Messages.INF_DB_BACKUP;
                return View(s);

            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View(s);
            }
        }
        public ActionResult Restore()
        {
            SystemViewModels s = new SystemViewModels();
            s.Server = ConfigurationManager.AppSettings["BkServer"];
            s.Database = ConfigurationManager.AppSettings["BkDatabase"];
            s.UserId = ConfigurationManager.AppSettings["BkUserId"];
            s.Password = ConfigurationManager.AppSettings["BkPassword"];
            s.BackupFiles = new List<SelectListItem>();
            string path = ConfigurationManager.AppSettings["PathBackup"];
            DirectoryInfo dir = new DirectoryInfo(path);

                foreach (FileInfo files in dir.GetFiles())
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = Path.GetFileNameWithoutExtension(files.FullName);
                    item.Value = files.Name;
                    s.BackupFiles.Add(item);
                }
            
            return View(s);
        }
        [HttpPost]
        public ActionResult Restore(SystemViewModels s)
        {
            try
            {
                UpdateModel(s);
                string server = s.Server;
                string db = s.Database;
                string uid = s.UserId;
                string pass = s.Password;
                s.BackupFiles = new List<SelectListItem>();
                string path = ConfigurationManager.AppSettings["PathBackup"];
                DirectoryInfo dir = new DirectoryInfo(path);
                foreach (FileInfo files in dir.GetFiles())
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = Path.GetFileNameWithoutExtension(files.FullName);
                    item.Value = files.Name;
                    s.BackupFiles.Add(item);
                } 

                string rootFolder = path+Request["ddlFiles"].ToString();
                DatabaseManager.RestoreDatabase(db, rootFolder, server, uid, pass);
                ViewData["MessageInfo"] = Messages.INF_DB_RESTORE;
                return View(s);

            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] =Messages.INF_DB_RESTORE_FAIL+ exp.Message;
                return View(s);
            }            
        }

    }
}
